package com.ruoyi.common.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.UUID;

/**
 *  图片上传工具类
 */
public class UploadImagesUtils {
    public static  String uploadImages(MultipartFile multipartFile,String url){
       String basePath = String.format("%s\\%d\\%d\\%d", url, LocalDate.now().getYear(), LocalDate.now().getMonthValue(), LocalDate.now().getDayOfMonth());
       File file = new File(basePath);
        if (!file.exists()){
            file.mkdirs();
        }
        // 获取文件名，拿到文件的后缀
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 使用UUID 生成文件名称
        String fileName = UUID.randomUUID() + suffix;
        // 拼接的图片路径
        String filePath = basePath +"\\"+ fileName;
        file = new File(filePath);
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return filePath;
    }

    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder();
        System.out.println(builder.toString());
    }
}
